package com.viphuli.app.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.viphuli.app.R;
import com.viphuli.common.MyPageHelper;
import com.viphuli.http.handler.PurchaseOrderPayWeichatResponseHandler;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	private IWXAPI api;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.weichat_pay_result);

		api = WXAPIFactory.createWXAPI(this, getString(R.string.weichat_app_id));
		api.handleIntent(getIntent(), this);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);

		api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {

	}

	@Override
	public void onResp(BaseResp resp) {

		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
				int code = resp.errCode;
				if (code == BaseResp.ErrCode.ERR_OK) {//支付成功
					//										ViewUtils.toast("支付成功");

					Bundle args = new Bundle();
					args.putString("order_id", PurchaseOrderPayWeichatResponseHandler.gainOrderId());
					args.putInt("pay_type", PurchaseOrderPayWeichatResponseHandler.gainPayType());
					MyPageHelper.purchasePaySuccess.showMyPage(this, args);
					finish();
				} else if (code == BaseResp.ErrCode.ERR_USER_CANCEL) {//用户取消(无需处理。发生场景：用户不支付了，点击取消，返回APP)
				//					ViewUtils.toast("用户取消");
					Bundle args = new Bundle();
					args.putString("order_id", PurchaseOrderPayWeichatResponseHandler.gainOrderId());
					args.putInt("pay_type", PurchaseOrderPayWeichatResponseHandler.gainPayType());
					MyPageHelper.purchasePayFails.showMyPage(this, args);
					finish();
				} else {//支付失败
					//					ViewUtils.toast("支付失败");

					Bundle args = new Bundle();
					args.putString("order_id", PurchaseOrderPayWeichatResponseHandler.gainOrderId());
					args.putInt("pay_type", PurchaseOrderPayWeichatResponseHandler.gainPayType());
					MyPageHelper.purchasePayFails.showMyPage(this, args);
					finish();
				}
			}
		}
	}
}